package com.example.adapter;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.database.DatabaseHelper;
import com.example.student.R;

public class CourseAdapter extends RecyclerView.Adapter<CourseAdapter.ViewHolder> {
    private Cursor cursor;
    private Context context;
    private DatabaseHelper databaseHelper;
    private Integer studentId;

    public CourseAdapter(Cursor cursor, Context context, Integer studentId) {
        this.cursor = cursor;
        this.context = context;
        this.studentId = studentId;
        this.databaseHelper = new DatabaseHelper(this.context);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_course, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {


        if (cursor.moveToPosition(position)) {
            // 获取字段的列索引
            int courseIdIndex = cursor.getColumnIndex("course_id");
            int teacherIdIndex = cursor.getColumnIndex("teacher_id");
            int startTimeIndex = cursor.getColumnIndex("start_time");
            int endTimeIndex = cursor.getColumnIndex("end_time");
            int creditsIndex = cursor.getColumnIndex("credits");
            int classroomIndex = cursor.getColumnIndex("classroom");
            int courseNameIndex = cursor.getColumnIndex("course_name");
            int teacherNameIndex = cursor.getColumnIndex("teacher_name");
            int weekIndex = cursor.getColumnIndex("week");
            int numWeekIndex = cursor.getColumnIndex("num_week");
            int collegeIndex = cursor.getColumnIndex("college");

            // 设置课程号
            if (courseIdIndex != -1) {
                String courseId = cursor.getString(courseIdIndex);
                holder.courseId.setText("课程号: " + courseId);
            }

            // 设置开始时间-结束时间
            if (startTimeIndex != -1 && endTimeIndex != -1 && weekIndex != -1 && numWeekIndex != -1) {
                String startTime = cursor.getString(startTimeIndex);
                String endTime = cursor.getString(endTimeIndex);
                String numWeek = cursor.getString(numWeekIndex);
                String week = cursor.getString(weekIndex);
                holder.time.setText("时间: " + numWeek+ " " + week + startTime + " - " + endTime);
            }

            // 设置学分
            if (creditsIndex != -1) {
                String credits = cursor.getString(creditsIndex);
                holder.credits.setText("学分: " + credits);
            }

            // 设置教室
            if (classroomIndex != -1) {
                String classroom = cursor.getString(classroomIndex);
                holder.classroom.setText("教室: " + classroom);
            }

            // 设置课程名称
            if (courseNameIndex != -1) {
                String courseName = cursor.getString(courseNameIndex);
                holder.courseName.setText(courseName);
            }

            // 设置教师姓名
            if (teacherNameIndex != -1) {
                String teacherName = cursor.getString(teacherNameIndex);
                holder.teacherName.setText("教师: " + teacherName);
            }
            if (courseIdIndex != -1 && teacherIdIndex != -1 && this.studentId != -1){
                int courseId = cursor.getInt(courseIdIndex);
                int teacherId = cursor.getInt(teacherIdIndex);
                if(databaseHelper.isEnrolled(courseId, teacherId, this.studentId)){
                    holder.btnEnroll.setText("退课");
                    holder.btnEnroll.setBackgroundColor(Color.RED);
                }else{
                    holder.btnEnroll.setText("选课");
                    holder.btnEnroll.setBackgroundColor(Color.BLUE);
                }

                holder.btnEnroll.setOnClickListener(v -> {
                    if(holder.btnEnroll.getText().equals("选课")){
                        databaseHelper.enrollCourse(courseId, teacherId,this.studentId);
                        holder.btnEnroll.setText("退课");
                        holder.btnEnroll.setBackgroundColor(Color.RED);
                    }else {
                        databaseHelper.unenrollCourse(courseId,teacherId,this.studentId);
                        holder.btnEnroll.setText("选课");
                        holder.btnEnroll.setBackgroundColor(Color.BLUE);
                    }

                });
            }

        }

    }

    @Override
    public int getItemCount() {
        return cursor.getCount();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView courseName, teacherName, teacherId, classroom, credits, time, courseId;
        Button btnEnroll;
        public ViewHolder(View itemView) {
            super(itemView);
            courseName = itemView.findViewById(R.id.courseName);
            teacherName = itemView.findViewById(R.id.teacherName);
            courseId = itemView.findViewById(R.id.courseId);
            classroom = itemView.findViewById(R.id.classroom);
            time = itemView.findViewById(R.id.time);
            credits = itemView.findViewById(R.id.credits);
            btnEnroll = itemView.findViewById(R.id.btnEnroll);
        }
    }

}